package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Transactional
    @Override//新增套餐
    public void saveWithSetmeal(SetmealDto setmealDto) {
        //保存套餐的基本信息,操作setmeal,执行insert操作
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息,操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override//删除套餐
    public void removeWithSetmeal(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        wrapper.in(Setmeal::getId, ids);
        wrapper.in(Setmeal::getStatus, 1);

        int count = this.count(wrapper);
        if (count > 0) {
            throw new CustomException("正在起售不能删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getId);
        setmealDishService.remove(wrapper1);
    }

    @Override//套餐回显
    public SetmealDto getWithSetmeal(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId, setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override//套餐修改
    public void updateWithDish(SetmealDto setmealDto) {
        /* 1、修改setmeal表中的基本数据*/
        this.updateById(setmealDto);
        /* 2、修改setmeal_dish表中的对应的菜品信息*/
        /* 2.1、删除旧的菜品信息*/
        //构建条件构造器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //配置天剑
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        //执行查询
        setmealDishService.remove(lqw);
        /*3、 添加提交的新的菜品数据*/
        // 3.1、获取当前提交的菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 3.2、遍历集合，为每一个菜品的setmealId属性赋值
        List<SetmealDish> list = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;

        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    //批量修改套餐状态，起售，停售
    @PostMapping("/status/{status}")
    public R<String> update(@PathVariable int status, @RequestParam List<Long> ids) {
        log.info(ids.toString());
        log.info(status + "");

        for (int i = 0; i < ids.size(); i++) {
            //根据id获取套餐对象
            Setmeal setmeal = setmealService.getById(ids.get(i));
            //将套餐对象的status属性的值，设置为status
            setmeal.setStatus(status);
            //调用service层方法 修改状态值
            setmealService.updateById(setmeal);

        }

        return R.success("修改状态成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }
}
